package com.southminority.ethnic.controller.admin.language;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.common.annotation.RequireAdmin;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.admin.language.vo.LanguageSentenceAdminVO;
import com.southminority.ethnic.controller.language.vo.LanguagePageResq;
import com.southminority.ethnic.controller.language.vo.PageKeyReqVo;
import com.southminority.ethnic.pojo.MinorityLanguageSentence;
import com.southminority.ethnic.service.language.MinorityLanguageSentenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 句子对比管理端控制器
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "语言数据库管理-句子对比管理", description = "句子对比管理")
@RestController
@RequestMapping("/admin/language-sentence")
@RequireAdmin(1)
public class LanguageSentenceAdminController {

    @Autowired
    private MinorityLanguageSentenceService service;

    /**
     * 分页查询句子对比
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询句子对比", description = "管理员分页查询句子对比数据")
    public R<Page<MinorityLanguageSentence>> page(@RequestBody PageKeyReqVo pageKeyReqVo) {
        try {
            Page<MinorityLanguageSentence> result = service.selectOnesPageKey(
                pageKeyReqVo.getPageNum(), 
                pageKeyReqVo.getPageSize(), 
                pageKeyReqVo.getKey(),
                    pageKeyReqVo.getMinorityIds()
            );
            return R.ok(result);
        } catch (Exception e) {
            log.error("分页查询句子对比失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 根据ID查询句子对比详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询句子对比详情", description = "根据ID查询句子对比详细信息")
    public R<LanguageSentenceAdminVO.DetailResponse> getById(@PathVariable Long id) {
        try {
            MinorityLanguageSentence sentence = service.getById(id);
            if (sentence == null) {
                return R.fail("句子对比不存在");
            }
            
            LanguageSentenceAdminVO.DetailResponse response = new LanguageSentenceAdminVO.DetailResponse();
            BeanUtils.copyProperties(sentence, response);
            
            return R.ok(response);
        } catch (Exception e) {
            log.error("查询句子对比详情失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 新增句子对比
     */
    @PostMapping("/add")
    @Operation(summary = "新增句子对比", description = "新增句子对比数据")
    public R<String> add(@Validated @RequestBody LanguageSentenceAdminVO.CreateRequest request) {
        try {
            MinorityLanguageSentence sentence = new MinorityLanguageSentence();
            BeanUtils.copyProperties(request, sentence);
            
            boolean success = service.add(sentence);
            if (success) {
                return R.ok("新增成功");
            } else {
                return R.fail("新增失败");
            }
        } catch (Exception e) {
            log.error("新增句子对比失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新句子对比
     */
    @PutMapping("/update")
    @Operation(summary = "更新句子对比", description = "更新句子对比数据")
    public R<String> update(@Validated @RequestBody LanguageSentenceAdminVO.UpdateRequest request) {
        try {
            MinorityLanguageSentence sentence = new MinorityLanguageSentence();
            BeanUtils.copyProperties(request, sentence);
            
            boolean success = service.updata(sentence);
            if (success) {
                return R.ok("更新成功");
            } else {
                return R.fail("更新失败");
            }
        } catch (Exception e) {
            log.error("更新句子对比失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除句子对比
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除句子对比", description = "根据ID删除句子对比数据")
    public R<String> delete(@Validated @RequestBody LanguageSentenceAdminVO.DeleteRequest request) {
        try {
            boolean success = service.delete(request.getId().intValue());
            if (success) {
                return R.ok("删除成功");
            } else {
                return R.fail("删除失败");
            }
        } catch (Exception e) {
            log.error("删除句子对比失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除句子对比
     */
    @DeleteMapping("/batch-delete")
    @Operation(summary = "批量删除句子对比", description = "批量删除句子对比数据")
    public R<String> batchDelete(@RequestBody Long[] ids) {
        try {
            if (ids == null || ids.length == 0) {
                return R.fail("请选择要删除的数据");
            }
            
            boolean success = service.removeByIds(java.util.Arrays.asList(ids));
            if (success) {
                return R.ok("批量删除成功");
            } else {
                return R.fail("批量删除失败");
            }
        } catch (Exception e) {
            log.error("批量删除句子对比失败", e);
            return R.fail(e.getMessage());
        }
    }
}
